package com.iims.market.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.market.domain.PurposeContract;
import com.iims.market.service.IPurposeContractService;
import com.iims.market.vo.ContactRowVo;
import com.iims.market.vo.GroupPurposeContractSearchQuery;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/16 16:16
 */
@Api(tags = "H-意向合同管理")
@RestController
@RequestMapping("/business/contract/purpose")
public class PurposeContractController extends BaseController {

    @Autowired
    private IPurposeContractService pContractService;

    /**
     * 获取合同列表
     */
    @ApiOperation("[高级搜索] 分页获取意向合同列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "创建开始时间:format = \"yyyy-MM-dd\"", name = "beginTime", paramType = "query"),
            @ApiImplicitParam(value = "创建结束时间:format = \"yyyy-MM-dd\"", name = "endTime", paramType = "query"),
            @ApiImplicitParam(value = "合同类型:精确匹配", name = "ctraTypeId", paramType = "query"),
            @ApiImplicitParam(value = "客户类型：精确匹配", name = "customerId", paramType = "query"),
            @ApiImplicitParam(value = "意向合同名称:模糊匹配", name = "contractName", paramType = "query"),
            @ApiImplicitParam(value = "合同编号:模糊匹配", name = "pcontractCode", paramType = "query"),
            @ApiImplicitParam(value = "销售人员:模糊匹配", name = "sellUser", paramType = "query")
    })
    @ApiOperationSupport(order = 1)
    @PreAuthorize("@ss.hasPermi('business:pContract:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore PurposeContract pContract) throws Exception {
        startPage();
        setSearchTimeRange(pContract);
        setSeqNumPageSizeStart(pContract);
        List<PurposeContract> list = pContractService.selectPContractList(pContract);
        return getDataTable(list);
    }

    /**
     * [分组搜索] 分页获取意向合同列表
     */
    @ApiOperation(value = "[分组搜索] 分页获取意向合同列表", notes = "页面按钮分组顺序：scopeValue、typeValue、timeValue", response = ContactRowVo.class)
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "综合模糊搜索:合同名/合同编号/合同类型/客户名/销售负责人", name = "searchValue", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:contract:list')")
    @GetMapping("/groupList")
    public TableDataInfo groupList(GroupPurposeContractSearchQuery groupSearchQuery) throws Exception {
        startPage();
        setSeqNumPageSizeStart(groupSearchQuery);
        return getDataTable(pContractService.selectPContractListGroup(groupSearchQuery));
    }

    /**
     * 根据意向合同编号获取详细信息
     */
    @ApiOperation("根据合同id获取合同详情")
    @ApiImplicitParam(name = "pContractId", value = "意向合同id", required = true, paramType = "query")
    @PreAuthorize("@ss.hasPermi('business:pContract:query')")
    @GetMapping(value = "/{pContractId}")
    public AjaxResult getInfo(@PathVariable Long pContractId) {
        return AjaxResult.success(pContractService.selectDetailById(pContractId));
    }

    /**
     * 新增意向合同
     */
    @ApiOperation(value = "新增意向合同", notes = "【腾讯文档】合同-baseReq字段说明\n" +
            "https://docs.qq.com/sheet/DUHNiVm9aVVpOeHZJ?tab=BB08J2")
    @ApiOperationSupport(ignoreParameters = {"purposeContractId", "ctraTypeName", "customerName", "sellUser"})
    @PreAuthorize("@ss.hasPermi('business:pContract:add')")
    @Log(title = "意向合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PurposeContract pContract) {
        return toAjax(pContractService.insertPContract(pContract));
    }

    /**
     * 修改意向合同
     */
    @ApiOperation("修改意向合同")
    @ApiOperationSupport(ignoreParameters = {"ctraTypeName", "customerName", "sellUser"})
    @PreAuthorize("@ss.hasPermi('business:pContract:edit')")
    @Log(title = "意向合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PurposeContract pContract) {
        return toAjax(pContractService.updatePContract(pContract));
    }

    /**
     * 删除意向合同
     */
    @ApiOperation("删除意向合同")
    @PreAuthorize("@ss.hasPermi('business:pContract:remove')")
    @Log(title = "意向合同管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pContractId}")
    public AjaxResult remove(@ApiParam("意向合同Id") @PathVariable Long pContractId) {
        return toAjax(pContractService.deletePContractById(pContractId));
    }
}
